package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.entity.Notice;
import com.example.entity.Params;
import com.example.service.NoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {

    private static final Logger log = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeService;

    @PostMapping
    @AutoLog("更新通知操作")
    public Result update(@RequestBody Notice notice){
        if (ObjectUtil.isNotEmpty(notice.getId())){
            noticeService.updateEntity(notice);
        }else {
            noticeService.add(notice);
        }
        return Result.success();
    }

    @GetMapping("/search")
    public Result findBySearch(Params params){
        return Result.success(noticeService.search(params));
    }

    @GetMapping
    public Result findTop3(){
        return Result.success(noticeService.findNotice());
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        return Result.success(noticeService.removeById(id));
    }


}

